CREATE TABLE [dbo].[tb_PartidasPresupuestarias]
(
[idPartida] [int] NOT NULL IDENTITY(1, 1),
[Descripcion] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Mnemo] [varchar] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[updatePartida] ON [dbo].[tb_PartidasPresupuestarias] 
FOR UPDATE
AS
SET NOCOUNT ON
if (update(idPartida)) begin
	UPDATE tb_Servicios 
	SET PartidaPresupuestaria = (SELECT idPartida from inserted)
	WHERE PartidaPresupuestaria in (SELECT idPartida from deleted)
	
	UPDATE tb_Insumos 
	SET PartidaPresupuestaria = (SELECT idPartida from inserted)
	WHERE PartidaPresupuestaria in (SELECT idPartida from deleted)

end

GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[updatetb_PartidasPresupuestarias] ON [dbo].[tb_PartidasPresupuestarias]
 FOR UPDATE AS 
SET NOCOUNT ON
 IF (update(idPartida)) begin 
	 SELECT * FROM inserted 
	 IF @@ROWCOUNT = 1 begin 
		 UPDATE tb_Plazas
		 SET Presupuesto = (SELECT idPartida FROM inserted)
		 WHERE tb_Plazas.Presupuesto = (SELECT idPartida FROM deleted)
		 UPDATE tb_AperturasDeReestructura
		 SET Presupuesto = (SELECT idPartida FROM inserted)
		 WHERE tb_AperturasDeReestructura.Presupuesto = (SELECT idPartida FROM deleted)
		 UPDATE tb_Servicios
		 SET PartidaPresupuestaria = (SELECT idPartida FROM inserted)
		 WHERE tb_Servicios.PartidaPresupuestaria = (SELECT idPartida FROM deleted)
		 UPDATE tb_Insumos
		 SET PartidaPresupuestaria = (SELECT idPartida FROM inserted)
		 WHERE tb_Insumos.PartidaPresupuestaria = (SELECT idPartida FROM deleted)
		 UPDATE tb_FuncionesLUI
		 SET Presupuesto = (SELECT idPartida FROM inserted)
		 WHERE tb_FuncionesLUI.Presupuesto = (SELECT idPartida FROM deleted)
	end  ELSE begin 
		 ROLLBACK TRANSACTION 
 end 
 end

GO
ALTER TABLE [dbo].[tb_PartidasPresupuestarias] ADD CONSTRAINT [PK_tb_PartidasPresupuestarias] PRIMARY KEY NONCLUSTERED  ([idPartida]) ON [PRIMARY]
GO
